﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using VPN.Manage;
using VPN.DataContext;

namespace VPNNew
{
    public partial class RenewPO : PageBase
    {
        private Guid accountID
        {
            get
            {
                object o = ViewState["AID"];
                return o == null ? Guid.Empty : (Guid)o;
            }
            set
            {
                ViewState["AID"] = value;
            }
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["AID"]))
                {
                    try
                    {

                        accountID = new Guid(Request.QueryString["AID"]);
                        Account a = AccountManage.GetAccountByAccountID(accountID);

                        if (Request.QueryString["Ignore"] != "1")
                        {
                            int notPaidPO = a.POs.Where(p => !p.HasPaid).Count();
                            if (notPaidPO > 0)
                            {
                                Server.Transfer(string.Format("~/POAssociationSelect.aspx?AID={0}", accountID), false);
                                return;
                            }
                        }

                        lblName.Text = a.AccountName;
                        lblType.Text = a.Type;
                        lblExpire.Text = a.ExpireDate == null ? string.Empty : a.ExpireDate.Value.ToString("yyyy-MM-dd");

                    }
                    catch (FormatException fe)
                    {
                        throw new ArgumentException(GRString("ErrorAccountArgument"), fe);
                    }
                }




                gvProduct.DataSource = ProductManage.GetProductList();
                gvProduct.DataBind();
            }
        }


        public override string PageCode
        {
            get { return "MyPO"; }
        }

        protected void btnBuy_Click(object sender, EventArgs e)
        {
            if (gvProduct.SelectedIndex != -1)
            {
                Product product = ProductManage.GetProductByID((Guid)gvProduct.DataKeys[gvProduct.SelectedIndex][0]);
                PO po = new PO()
                    {
                        AccountID = accountID,
                        CreateDate = DateTime.Now,
                        ProductID = product.ProductID,
                        OringinalPrice = product.Price,
                        POID = Guid.NewGuid(),
                        PONumber = string.Format("{0}-{1}-{2}",
                            DateTime.Now.ToString("yyyyMMddHHmm")
                            , product.ProductCode
                            , Math.Abs(Guid.NewGuid().GetHashCode()).ToString().Substring(0, 6)
                            ),
                        UserName = Page.User.Identity.Name,
                    };
                POManage.Create(po);
                Response.Redirect(string.Format("~/POConfirm.aspx?POID={0}", po.POID));
            }
        }




    }
}
